<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;

use App\Http\Repository\OrderRepository;
use App\Models\news;
use App\Models\Order;
use App\Services\Token;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    //
    public function Order(Request $request)
    {
        $data = $request->all();
        $token = $data['token'];
        $id = (new Token())->chekToken($token);
        $data['order'] = time().rand(1111111,9999999);

        Order::insert(['order_no'=>$data['order'],'user_id'=>$id['user_id'],'payment'=>$data['price'],'created_at'=>date("Y-m-d H:i:s ")]);
        return view('api/index/index',['data'=>$data]);
    }
    /**
     * @param Request $request
     * @return false|string
     * Su ang
     * 支付回调地址
     */
    public function zfcallback(Request $request)
    {
        $order = $request['out_trade_no'];
        $id = Order::where('order_no',$order)->value('user_id');
        news::news($id,'你好，你的订单'.$order.'支付成功');
        Order::where('order_no',$order)->update(['status'=>1,'pay_time'=>date("Y-m-d H:i:s")]);
        echo '恭喜你，支付成功';
    }

    /**
     * @param Request $request
     * @return false|string
     * Su ang
     * 查询用户订单
     */
    public  function orderQuery(Request $request)
    {
        $token = $request['token'];
        $id = (new Token())->chekToken($token);
       $data =  OrderRepository::orderQuery($id['user_id']);
       return $data;
    }
}
